PBDS Hash
ย - Last update: 2024-08-18

PBDS Hash์— ๊ด€ํ•œ ๊ฐ„๋‹จํ•œ ์ •๋ฆฌ.

PBDS Hash?

PBDS๋Š” Policy based data structures์˜ ์•ฝ์ž๋กœ, g++์—์„œ ext/pb_ds ์•„๋ž˜์— ์œ„์น˜ํ•˜๋Š” ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ๋ฅผ ์นญํ•œ๋‹ค. ์œ„ ์ฝ”๋“œํฌ์Šค ๋ธ”๋กœ๊ทธ ๊ธ€์„ ์ฐธ์กฐํ•˜๋ฉด, ์ผ๋ฐ˜์ ์ธ unordered_map ๋ณด๋‹ค ํ›จ์”ฌ ๋น ๋ฅธ ์†๋„๋กœ ๋™์ž‘ํ•œ๋‹ค๊ณ  ํ•œ๋‹ค.

PBDS์—์„œ ์‚ฌ์šฉ๊ฐ€๋Šฅํ•œ ๊ฒƒ์€ gp_hash_table ๊ณผ cc_hash_table ์ธ๋ฐ, gp_hash_table์€ Open Addressing ๋ฐฉ์‹์ด๊ณ , cc_hash_table์€ Chaining ๋ฐฉ์‹์ด๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ ์†๋„๋ฅผ ๋†’ํžˆ๊ธฐ ์œ„ํ•ด์„œ PBDS๋ฅผ ๊ฐ€์ง€๊ณ  ์˜ค๋Š” ๊ฒƒ์ด๋ฏ€๋กœ ์„ฑ๋Šฅ ํ–ฅ์ƒ์„ ๋ชฉ์ ์œผ๋กœ๋Š” gp_hash_table์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ๋งž๊ฒ ๋‹ค.

์‚ฌ์šฉ ๋ฐฉ๋ฒ•

ํ—ค๋”๋ฅผ ๋„ฃ๊ณ , ์ผ๋ฐ˜ unordered_map ์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค. ๋. atcoder ์— ์ƒ˜ํ”Œ๋กœ ์ œ์ถœํ•œ ์†”๋ฃจ์…˜์€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
const int HW_RANDOM = chrono::high_resolution_clock::now().time_since_epoch().count();
struct chash {
int operator()(int x) const { return x ^ HW_RANDOM; }
};
gp_hash_table<int, int, chash> Table;

์—ฌ๊ธฐ์„œ chash ๋Š” ํ•ด์‹œ ์ €๊ฒฉ ๋ฐฉ์ง€์šฉ์ด๋‹ค. ๊ฐ„๋‹จํ•˜๊ฒŒ ์ €๊ฒฉ ์ƒ๊ฐํ•˜์ง€ ์•Š๊ณ  ๊ทธ๋ƒฅ ์“ธ๊ฑฐ๋ฉด ์•„๋ž˜์ฒ˜๋Ÿผ๋„ ๊ฐ€๋Šฅํ•˜๋‹ค.

#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
gp_hash_table<int, int> Table;

์‚ฌ์šฉ์€ STL๊ณผ ๊ฑฐ์˜ ๊ฐ™์œผ๋ฏ€๋กœ, ํŽธํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

๐Ÿท๏ธ ์ฃผ์ œ ๋ชฉ๋ก: